package com.ms.service.module.zone.action;


import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;

import com.xpec.c4.game.common.LoginVerifyCodeManager;
import com.xpec.c4.game.common.VerifyInfoNotifyMsg;
import com.xpec.c4.game.login.LoginModule;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.In;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.annotations.Name;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.contexts.ScopeType;



@Name(scopeType=ScopeType.SESSION)
public class ZoneVerifyAction {
	
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(ZoneVerifyAction.class);

	@In
	public GUID sessionID;

	
	@MessageType(typeCode = MessageTypes.S_LOGIN_VERIFYCODE_REQUEST)
	public void onRecvLoginVerifyCodeRequest(VerifyInfoNotifyMsg info){
		int result;
		if(info == null  || info.getVerifyInfo() == null || Contexts.getModule() == null)
			return ;

		//TODO:Mike說   換zone的時候，先判斷zone滿了沒，可寫在此
		result = VerifyInfoNotifyMsg.VERIFY_SUCCESS;
		
		
		info.setResult(result);
		//存放verify資料
		if(result == VerifyInfoNotifyMsg.VERIFY_SUCCESS){
			LoginVerifyCodeManager.getInstance().putVerifyCode(info.getVerifyInfo().getUserId(), info.getVerifyInfo());
		}

		if(log.isInfoEnabled()){
			log.info(LoginModule.LOGIN_TRACE_LOG_STRING+" send [S_LOGIN_VERIFYCODE_RESPONSE] s: "+info.getSessionID().toString()+" /g: "+info.getVerifyInfo().getUserId().toString());
		}
		LoginVerifyCodeManager.getInstance().sendVerifyCodeNotifyResponseMsg(info);

	}
	
	
	/**
	 * 玩家通知不過來了, 把他的verifyCode移除以免卡位(段線時通知
	 * 斷線時要記得呼叫, 將verifyCode清除
	 * @param param
	 */
	@MessageType(typeCode = MessageTypes.S_ZONE_PLAYER_CANCEL_LOGIN_REQUEST)	
	public void onRecvPlayerCancelLoginRequest(GUID guid){
		if(guid == null || Contexts.getModule() == null ) return;
		
		String  zoneId = Contexts.getModule().getModuleName(); 
		LoginVerifyCodeManager.getInstance().removeVerifyCode(guid, zoneId);
	}
}
